﻿using Microsoft.WindowsAPICodePack.Dialogs;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WPF.DEMO.Pages
{
    using SharpSoft.WPF;
    /// <summary>
    /// SequenceImagePage.xaml 的交互逻辑
    /// </summary>
    public partial class SequenceImagePage : Page
    {
        public SequenceImagePage()
        {
            InitializeComponent();
        }

        private async void btn_folder_Click(object sender, RoutedEventArgs e)
        {
            var dialog = new CommonOpenFileDialog();
            dialog.IsFolderPicker = true;
            CommonFileDialogResult result = dialog.ShowDialog();
            if (result == CommonFileDialogResult.Ok)
            {
                var folder = dialog.FileName;
                if (System.IO.Directory.GetFiles(folder, "*.png").Length == 0)
                {
                    await this.SampleAsync($"{folder}中没有png图像文件。", ThemeColor.Error);
                    return;
                }
                txt_folder.Text = folder;

            }
        }

        private async void btn_gen_Click(object sender, RoutedEventArgs e)
        {
            var folder = txt_folder.Text;
            if (System.IO.Directory.GetFiles(folder, "*.png").Length == 0)
            {
                await this.SampleAsync($"{folder}中没有png图像文件。", ThemeColor.Error);
                return;
            }

            var dialog = new CommonSaveFileDialog();
            dialog.EnsureReadOnly = true;
            dialog.Filters.Add(new CommonFileDialogFilter("sip序列帧文件", "*.sip"));
            CommonFileDialogResult result = dialog.ShowDialog();
            if (result == CommonFileDialogResult.Ok)
            {
                var win = this.GetWindow();
                var wait = await win.WaitAsync("正在生成...");
                try
                {
                    var frameRate = (int)nud.Value;
                    var filename = dialog.FileName;
                    var info = await Task.Run(delegate
                      {
                          return SharpSoft.WPF.Controls.SequenceImage.GenerateSequenceImageFile(folder, filename, frameRate, -1);
                      });
                    await LoadFile(filename);
                }
                catch (Exception ex)
                {
                    await win.SampleAsync(ex.Message, ThemeColor.Error);
                }
                finally
                {
                    wait.Close();
                }
            }
        }

        private async void btn_load_Click(object sender, RoutedEventArgs e)
        {
            var dialog = new CommonOpenFileDialog();
            dialog.EnsureReadOnly = true;
            dialog.Filters.Add(new CommonFileDialogFilter("sip序列帧文件", "*.sip"));
            CommonFileDialogResult result = dialog.ShowDialog();
            if (result == CommonFileDialogResult.Ok)
            {
                var filename = dialog.FileName;

                await LoadFile(filename);

            }
        }

        private async Task LoadFile(string filename)
        {
            try
            {
                si.OpenFile(filename);
                si.Play();
                var finfo = new System.IO.FileInfo(filename);
                await this.SampleAsync($"文件路径：{filename}\n文件大小：{finfo.Length / (1024d * 1024):0.00M}\n帧数：{si.SequenceInfo.FramesCount}帧。\n单帧大小：{si.SequenceInfo.FrameDataSize / (1024d * 1024):0.00M}\n默认帧率：{si.SequenceInfo.DefaultFramerate}", ThemeColor.Info);
            }
            catch (Exception)
            {
                await this.SampleAsync($"{filename}加载序列帧文件失败。", ThemeColor.Error);
                return;
            }
        }

        private void rb_white_Checked(object sender, RoutedEventArgs e)
        {
            if (IsLoaded)
            {
                grid.Background = new SolidColorBrush(Color.FromArgb(200, 255, 255, 255));
            }

        }

        private void rb_black_Checked(object sender, RoutedEventArgs e)
        {
            if (this.IsLoaded)
            {
                grid.Background = new SolidColorBrush(Color.FromArgb(200, 0, 0, 0));
            }

        }
    }
}
